/*
FlashSound javascript class for sonifying web pages with flash player
copyright 2001 Hayden Porter, hayden@aviarts.com
v 1.1.5 last update May 11, 2001
*/

// defaults to hiding browser error messages
FlashSound.muteErrorMsg = true;

function errorHandler()
{
	return FlashSound.muteErrorMsg;
}

window.onerror = errorHandler;

// browser compatibility check (support for level 4 browsers only) -----------------
FlashSound.winIEpass = 	((navigator.appName.indexOf("Microsoft") != -1) && 
						(navigator.appVersion.indexOf("Windows") != -1)) && 
						(parseFloat(navigator.appVersion) >= 4) ? true : false;
			
FlashSound.NNpass = 	((navigator.appName == "Netscape") && 
						(navigator.userAgent.indexOf("Mozilla") != -1) && 
						(parseFloat(navigator.appVersion) >= 3) && 
						(navigator.javaEnabled())) ? true : false;
				
FlashSound.supportedBrowser = (FlashSound.winIEpass || FlashSound.NNpass) ? true : false;

// player compatibility  ------------------

// check for flash plug-in in netscape
function Flash_checkForPlugIn()
{
	var flashmimeType = "application/x-shockwave-flash";
	var hasplugin = (navigator.mimeTypes && navigator.mimeTypes[flashmimeType]) ? navigator.mimeTypes[flashmimeType].enabledPlugin : 0;
	return hasplugin;
}

function Flash_getPlugInVers()
{
	if(Flash_checkForPlugIn())
	{
		var plugin = navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin;
		var pluginversion = parseInt(plugin.description.substring(plugin.description.indexOf(".")-1))
		return pluginversion;
	}
	else 
	{
		return 0;
	}
}

//NN6 does not support javscript api with liveconnect flash players
if ((navigator.appName == "Netscape") && 
	(navigator.userAgent.indexOf("Mozilla") != -1) && 
	(parseFloat(navigator.appVersion) >= 6) && 
	(Flash_getPlugInVers() <= 5))
	{FlashSound.supportedBrowser = false}

// vbscript get Flash ActiveX control version for windows IE
if(FlashSound.supportedBrowser && FlashSound.winIEpass)
{
	document.write(
		'<script language=VBScript>' + '\n' +
		'Function Flash_getActiveXVersion()' + '\n' +
			'On Error Resume Next' + '\n' +
			'Dim hasPlayer, playerversion' + '\n' +
			'hasPlayer = false' + '\n' +
			'playerversion = 15' + '\n' +
			'Do While playerversion > 0' + '\n' +
				'hasPlayer = (IsObject(CreateObject(\"ShockwaveFlash.ShockwaveFlash.\" & playerversion)))' + '\n' +
				'If hasPlayer Then Exit Do' + '\n' +
				'playerversion = playerversion - 1' + '\n' +
			'Loop' + '\n' +
			'Flash_getActiveXVersion = playerversion' + '\n' +
		'End Function' + '\n' +
		'<\/script>'
		);
}

// set playerVersion to 0 for unsupported browsers
// checkForMinPlayer sets playerVersion for supported browsers
FlashSound.playerVersion = 0;
function Flash_checkForMinPlayer()
{
	if(!FlashSound.supportedBrowser) return false;
	if(FlashSound.NNpass) {FlashSound.playerVersion = Flash_getPlugInVers();}
	if(FlashSound.winIEpass) {FlashSound.playerVersion = (Flash_getActiveXVersion());}
	if(FlashSound.playerVersion >= FlashSound.minPlayer) {return true}
	else{return false}
}

// vers is integer
function Flash_setMinPlayer(vers)
{
	if(!FlashSound.supportedBrowser) return
	FlashSound.minPlayer = (vers != null && vers >= 4) ? vers : 4;
	FlashSound.checkForMinPlayer();
}

// code is string
function Flash_ifNotMinPlayer(code)
{
	if(!FlashSound.supportedBrowser) return
	if(!FlashSound.checkForMinPlayer())
	{
		eval(code);
	}
}

FlashSound.checkForMinPlayer = Flash_checkForMinPlayer;
FlashSound.setMinPlayer = Flash_setMinPlayer;
FlashSound.setMinPlayer();

FlashSound.ifNotMinPlayer = Flash_ifNotMinPlayer;

/* ============== FlashSound Instance methods =============== */

/*
javascript embed ---------------------------------
embeds swf if user has a supported browser and minimum player.
script sets swf bgcolor attribute to document.bgcolor if no custom color specified.
*/
function Flash_embedSWF(srcURL)
{
	if (!FlashSound.supportedBrowser || !FlashSound.checkForMinPlayer()) return;
	
	var defaultColor = (document.bgColor != null) ? document.bgColor : "#ffffff";
	var defaultBase = "";
	this.bgcolor = (this.bgcolor == null) ? defaultColor : this.bgcolor;
	this.base = (this.base == null) ? defaultBase : this.base; // default base and quotes for base parameter
	this.src = (srcURL.charAt(0) == "/") ? location.host+srcURL : srcURL; // root relative urls
	document.write(
		'<OBJECT' + '\n' +
			'classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\"' + '\n' +
 			'codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab\"' + '\n' +
 			'WIDTH=1' + '\n' +
			'HEIGHT=1' + '\n' +
			'ID=\"' + this.playerID + '\">' + '\n' +
    		'<PARAM NAME=movie VALUE=\"' + this.src + '\">' + '\n' +
			'<PARAM NAME=play VALUE=\"' + this.autostart + '\">' + '\n' +
			'<PARAM NAME=loop VALUE=\"' + this.loop + '\">' + '\n' +
    		'<PARAM NAME=quality VALUE=low>' + '\n' +
    		'<PARAM NAME=wmode VALUE=transparent>' + '\n' +
    		'<PARAM NAME=bgcolor VALUE=' + this.bgcolor + '>' + '\n' +
			'<PARAM NAME=base VALUE=\"' + this.base + '\">' + '\n' +
    		'<EMBED' + '\n' +
				'name=\"' + this.playerID + '\"' + '\n' +
				'swLiveConnect=\"true\"' + '\n' +
				'src=\"' + this.src + '\"' + '\n' +
				'play=\"' + this.autostart + '\"' + '\n' +
				'loop=\"' + this.loop + '\"' + '\n' +
				'quality=low' + '\n' +
				'wmode=transparent' + '\n' +
				'base=\"' + this.base + '\"' + '\n' +
				'bgcolor=' + this.bgcolor + '\n' +
				'WIDTH=1' + '\n' +
				'HEIGHT=2' + '\n' +
				'TYPE=\"application/x-shockwave-flash\"' + '\n' +			
				'PLUGINSPAGE=\"http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash\">' +
				'\n' +
    		'</EMBED>' + '\n' +
  		'</OBJECT>'
		);
}

/* 
check for player readiness ----------------------
check for javascript object first then check to see if any frames are loaded in maintimeline
*/
function Flash_checkForInstance()
{
	if(!FlashSound.supportedBrowser || !FlashSound.checkForMinPlayer()) {return false;}
	if (window.document[this.playerID] == null) {return false;}
	return true;
}

function Flash_isPlayerReady()
{
	if(!this.checkForInstance()) {return false;}
	if(this.PercentLoaded() > 0) {return true;}
	return false;
}

function Flash_GetFramesLoaded(target)
{
	if(!this.checkForInstance()) {return 0;}
	if(target == null) target = "/";
	var framesloaded = window.document[this.playerID].TGetProperty(target,12);
	return parseInt(framesloaded);
}

function Flash_GetTotalFrames(target)
{
	if(!this.isPlayerReady()) {return 0;}
	if(target == null) target = "/";
	var totalframes = window.document[this.playerID].TGetProperty(target,5);
	return parseInt(totalframes);
}

/*
check to see if all frames for a given timeline are loaded.
check before moving playhead to a frame/label incase the frame/label is not yet loaded.
*/
function Flash_TLoaded(target)
{
	if(!this.isPlayerReady()) {return false;}
	if(target == null) {target = "/";}
	if (this.getFramesLoaded(target) == this.getTotalFrames(target)) {return true;}
	return false;
}

/*
flash javascript api functions ------------------------
*/

function Flash_gotoAndPlay(target,frame)
{
	if(!this.isPlayerReady()) {return}
	if(typeof(frame) == "number")
	{
		window.document[this.playerID].TGotoFrame(target,frame - 1);
		window.document[this.playerID].TPlay(target);
	}
	if(typeof(frame) == "string")
	{
		window.document[this.playerID].TGotoLabel(target,frame);
		window.document[this.playerID].TPlay(target);
	}
}

function Flash_gotoAndStop(target,frame)
{
	if(!this.isPlayerReady()) {return}
	if(typeof(frame) == "number")
	{
		window.document[this.playerID].TGotoFrame(target,frame - 1);
	}
	if(typeof(frame) == "string")
	{
		window.document[this.playerID].TGotoLabel(target,frame);
	}
}

function Flash_api_PercentLoaded()
{
	if(!this.checkForInstance()) {return 0;}
	var percentLoaded = window.document[this.playerID].PercentLoaded();
	return parseInt(percentLoaded);
}

function Flash_api_TPlay(target)
{
	if(!this.isPlayerReady()) {return}
	window.document[this.playerID].TPlay(target);
}

function Flash_api_TStopPlay(target)
{
	if(!this.isPlayerReady()) {return;}
	window.document[this.playerID].TStopPlay(target);
}

function Flash_api_IsPlaying()
{
	if(!this.isPlayerReady()) {return false;}
	return window.document[this.playerID].IsPlaying();
}

// layerNumber is integer, url is string
function Flash_api_LoadMovie(layerNumber,url)
{
	if(!this.isPlayerReady()) {return;}
	window.document[this.playerID].LoadMovie(layerNumber,url);
}

/*
flash sound object constructor function ---------------------------
*/

FlashSound.players = 0;
function FlashSound()
{
	// instance properties
	FlashSound.players++
	this.playerID = "FlashSound_swf" + FlashSound.players;
	
	// instance embed properties
	this.autostart = true;
	this.base = null;
	this.bgcolor = null;
	this.loop = false;
	this.src = null;
	
	// instance methods
	this.embedSWF = Flash_embedSWF;
	this.checkForInstance = Flash_checkForInstance;
	this.isPlayerReady = Flash_isPlayerReady;
	this.getFramesLoaded = Flash_GetFramesLoaded;
	this.getTotalFrames = Flash_GetTotalFrames;
	this.TLoaded = Flash_TLoaded
	this.TGotoAndPlay = Flash_gotoAndPlay;
	this.TGotoAndStop = Flash_gotoAndStop;
	
	// flash javascript api alias methods
	this.PercentLoaded = Flash_api_PercentLoaded;
	this.TPlay = Flash_api_TPlay;
	this.TStopPlay = Flash_api_TStopPlay;
	this.IsPlaying = Flash_api_IsPlaying;
	this.LoadMovie = Flash_api_LoadMovie;
}